package by.yakusekmaxim.bookgid.webapp.page.home.book;

import java.util.Date;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Review;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.enums.PublicationStatus;
import by.yakusekmaxim.bookgid.services.ReviewService;
import by.yakusekmaxim.bookgid.webapp.app.BasicAuthenticationSession;

public class NewReviewPanel extends Panel {

	@Inject
	ReviewService reviewService;

	private UserAccount user = BasicAuthenticationSession.get().getUser();

	private Review review;

	private ResourceModel addResource;

	public NewReviewPanel(String id, final Book book) {
		super(id);
		if (reviewService.getReview(book, user) == null) {
			review = new Review();
			review.setBook(book);
			review.setStatus(PublicationStatus.publicationstatus_published);
			review.setUserAccount(user);
			addResource = new ResourceModel("p.home.bookdetails.addReview");
		} else {
			review = reviewService.getReview(book, user);
			addResource = new ResourceModel("p.home.bookdetails.editReview");
		}

		Form<Review> form = new Form<Review>("form",
				new CompoundPropertyModel<Review>(review)) {
			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof EventReview) {
					EventReview eventReview = (EventReview) payload;
					if (eventReview.getForm() != null) {
						this.add(eventReview.getForm());
						eventReview.getTarget().add(this);
					}
				}

			}
		};

		TextArea<String> text = new TextArea<String>("text");
		OnChangeAjaxBehavior radioChoiceAjixUpdade = new OnChangeAjaxBehavior() {

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				System.out.println(review.getText());
				EventReview eventReview = new EventReview();
				eventReview.setTarget(target);
				if (review.getText() != null) {
					eventReview.setSend(true);
				} else {
					eventReview.setSend(false);
				}
				send(getPage(), Broadcast.BREADTH, eventReview);
			}
		};
		text.add(radioChoiceAjixUpdade);
		text.add(new PropertyValidator<String>());
		form.add(text);

		SubmitLink send = new SubmitLink("Send") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				review.setDate(new Date());
				reviewService.saveOrUpdate(review);

				BookDetailsPage page = new BookDetailsPage(book);
				setResponsePage(page);
			}

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof EventReview) {
					EventReview eventReview = (EventReview) payload;
					if (eventReview.getSend() != null) {
						this.setEnabled(eventReview.getSend());
						eventReview.getTarget().add(this);
					}
				}

			}
		};
		send.setOutputMarkupId(true);
		send.setEnabled(false);
		form.add(send);

		form.add(new AjaxLink<Void>("cancel") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				EventReview eventReview = new EventReview();
				eventReview.setTarget(target);
				eventReview.setForm(AttributeModifier.replace("style",
						"display: none;"));
				eventReview.setAdd(AttributeModifier.replace("style",
						"display: block;"));
				send(getPage(), Broadcast.BREADTH, eventReview);
			}
		});

		form.add(AttributeModifier.replace("style", "display: none;"));

		form.add(new Label("labelh3", addResource));

		add(form);

		AjaxLink<Void> add = new AjaxLink<Void>("add") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				this.add(AttributeModifier.replace("style", "display: none;"));
				target.add(this);

				EventReview eventReview = new EventReview();
				eventReview.setTarget(target);
				eventReview.setForm(AttributeModifier.replace("style",
						"display: block;"));
				send(getPage(), Broadcast.BREADTH, eventReview);
			}

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof EventReview) {
					EventReview eventReview = (EventReview) payload;
					if (eventReview.getAdd() != null) {
						this.add(eventReview.getAdd());
						eventReview.getTarget().add(this);
					}
				}

			}
		};

		add.add(new Label("addLabel", addResource));
		add(add);

	}

}
